package com.hepinghospital.user.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseDialogFragment;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2022/8/10 14:55
 */
public class ConsultEvaluateDialog extends BaseDialogFragment {

    private String appointmentId;

    public ConsultEvaluateDialog() {
    }

    public static ConsultEvaluateDialog newInstance(String appointmentId) {
        Bundle args = new Bundle();
        args.putString("appointmentId", appointmentId);
        ConsultEvaluateDialog fragment = new ConsultEvaluateDialog();
        fragment.setArguments(args);
        return fragment;
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private EditText etEvaluate;
    private String score;

    @SuppressLint("WrongViewCast")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_nurse_consult, container);
        appointmentId = getArguments().getString("appointmentId");
        RatingBar ratingBar1 = view.findViewById(R.id.ratingBar);
        ImageView btn_cancel = view.findViewById(R.id.btn_cancel);
        TextView evaluateTv = view.findViewById(R.id.evaluate_tv);
        TextView tvSubmitComment = view.findViewById(R.id.tv_submit_comment);
        etEvaluate = view.findViewById(R.id.et_evaluate);
        btn_cancel.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onCancel();
                dismiss();
            }
        });
        tvSubmitComment.setOnClickListener(v -> evaluateConsultOrder());

        ratingBar1.setOnRatingBarChangeListener((ratingBar, rating, fromUser) ->{
            score = String.valueOf((int) rating);
            evaluateTv.setText(score+"星");
        } );
        return view;
    }


    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
//        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm();

        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }

    /**
     * 评价
     */
    private void evaluateConsultOrder() {
        RestProxy.getInstance().evaluateConsultOrder(appointmentId, etEvaluate.getText().toString(), score, new BaseObserver<String>() {
            @Override
            public void onSuccess(String result) {
                mListener.onConfirm();
                dismiss();
            }

            @Override
            public void onError(@NonNull Throwable e) {
                super.onError(e);
                ToastUtil.showToast(mContext,e.getMessage());

            }
        });
    }
}
